$(function() {
	
	$('#grabTable').bootstrapTable({
		data : getDatas(),
		striped : true,
		cache : false,
		pagination : true,
		sortable : false,
		toolbar : "#grabeToolBar",
		sidePagination : "client",
		pageNumber : 1,
		pageSize : 10,
		pageList : [ 10, 25, 50, 100 ],
		search : true,
		strictSearch : false,
		showColumns : true,
		clickToSelect : true,
		uniqueId : 'id',
		columns : [ 
		{
			field : 'customerName',
			title : '报修客户'
		}, {
			field : 'deviceName',
			title : '报修设备'
		}, {
			field : 'billDescHideOverflow',
			title : '损坏描述'
		}, {
			field : 'billAddressHideOverflow',
			title : '维修地址'
		}, {
			field : 'operate',
			title : '操作'
		} ],
		onDblClickRow : function(row) {// 双击查看详情
			showRepairDetail(row);
		}
	});

	$("#grabTable").bootstrapTable("hideColumn", "id");
});

function getDatas() {
	var bills = '';
	$.ajax({
		url : "../bill/query.action",
		type : "post",
		async : false,// 是为了内层ajax在之后访问
		data: {"billStatus":"3"},//待抢单为 3
		success : function(data) {
			$(data).each(function() {
				this.billDescHideOverflow = '<div class=\'td-cont-3\'>'+this.billDesc + '</div>';
				this.billAddressHideOverflow = '<div class=\'td-cont-3\'>'+this.billAddress + '</div>';
				this.operate = '<button class=\'btn btn-primary btn-xs\' onclick=\'grab('
						+ this.id + ',' + '1'
						+ ')\'>抢单</button>&nbsp;';
			});
			verifys = data;
		},
		error : function() {
			toastr.error("获取报修信息发生出错，请联系管理员");
		}
	});
	return verifys.reverse();
}

//详情
function showRepairDetail(bill) {
	
	var contract = getContractById(bill.contractId);
	
	var billDetailHtml = '<p style="text-align: center; color: #6294AD;"><b>基本信息</b></p>'
		+	'<p>id：' + bill.id + '</p>'
		+	'<p>客户姓名：' + bill.customerName + '</p>'
		+	'<p>报修设备：' + bill.deviceName + '</p>'
		+	'<p>损坏描述：<pre style="white-space: pre-wrap !important;word-wrap: break-word !important;">' + bill.billDesc + '</pre></p>'
		+	'<p>维修地址：<pre style="white-space: pre-wrap !important;word-wrap: break-word !important;">' + bill.billAddress + '</pre></p>'
		+	'<p>损坏图片：<div class="detailImageDiv"><img style="display:block; margin:0 auto;" src="' + bill.billImageUrl + '"/><div></p>';
	
		
	
	var device = getDeviceById(contract.deviceId);
	billDetailHtml += '<hr/><p style="text-align: center; color: #6294AD;"><b>设备信息</b></p>'
		+	'<p>设备名称：' + device.deviceName + '</p>'
	
	$.ajax({
		url:"../deviceAttr/query.action",
		type: "post",
		data: {"deviceId":device.id, "attrStatus":"1"},
		async: false,
		success: function(data) {
			$(data).each(function(){
				billDetailHtml += '<p>'+getAttrNameName(this.attrNameId)+'：'+this.attrValue+'</p>';
			});
		}
	});
		
	billDetailHtml += '<p>售后服务号：' + device.deviceServiceNumber + '</p>'
		+	'<p>设备描述：<pre style="white-space: pre-wrap !important;word-wrap: break-word !important;">' + device.deviceDesc + '</pre></p>'
		+	'<p>设备图片：<div class="detailImageDiv"><img style="display:block; margin:0 auto;" src="' + device.deviceImageUrl + '"/><div></p>';

	$("#repairDetailShow").html(billDetailHtml);
	$("#repairDetailModal").modal("show");
}

function grab(billId) {
	swal({
        title: "您确定要抢接所选维修单吗",
        text: "接单后请尽快上门维修！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "接单",
        closeOnConfirm: false
    }, function () {
    	$.ajax({
    		url: "../repair/grabSave.action",
    		type: "post",
    		data: {"billId":billId},
    		success: function (data) {
    			if (data.success == "true") {
    				swal("抢单成功", "您已经接手此维修单。", "success");
    				$("#grabTable").bootstrapTable("load", getDatas());
    			} else {
    				swal("抢单失败", "后台处理发生错误，请联系管理员", "error");
    			}
    		},
    		error: function() {
    			swal("抢单失败", "提交请求发生错误，请联系管理员。", "error");
    		}
    	});
    				
    });
	
}
